home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / config.js < prev    next >
Encoding:
Text File  |  2005-09-20  |  16.4 KB  |  541 lines

  1. //@line 40 "/c/mozilla/toolkit/components/viewconfig/content/config.js"
  2.  
  3. const nsIPrefLocalizedString = Components.interfaces.nsIPrefLocalizedString;
  4. const nsISupportsString = Components.interfaces.nsISupportsString;
  5. const nsIPromptService = Components.interfaces.nsIPromptService;
  6. const nsIPrefService = Components.interfaces.nsIPrefService;
  7. const nsIPrefBranch = Components.interfaces.nsIPrefBranch;
  8. const nsIClipboardHelper = Components.interfaces.nsIClipboardHelper;
  9. const nsIAtomService = Components.interfaces.nsIAtomService;
  10.  
  11. const nsSupportsString_CONTRACTID = "@mozilla.org/supports-string;1";
  12. const nsPrompt_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
  13. const nsPrefService_CONTRACTID = "@mozilla.org/preferences-service;1";
  14. const nsClipboardHelper_CONTRACTID = "@mozilla.org/widget/clipboardhelper;1";
  15. const nsAtomService_CONTRACTID = "@mozilla.org/atom-service;1";
  16.  
  17. const gPromptService = Components.classes[nsPrompt_CONTRACTID].getService(nsIPromptService);
  18. const gPrefService = Components.classes[nsPrefService_CONTRACTID].getService(nsIPrefService);
  19. const gPrefBranch = gPrefService.getBranch(null).QueryInterface(Components.interfaces.nsIPrefBranch2);
  20. const gClipboardHelper = Components.classes[nsClipboardHelper_CONTRACTID].getService(nsIClipboardHelper);
  21. const gAtomService = Components.classes[nsAtomService_CONTRACTID].getService(nsIAtomService);
  22.  
  23. var gLockAtoms = [gAtomService.getAtom("default"), gAtomService.getAtom("user"), gAtomService.getAtom("locked")];
  24. // we get these from a string bundle
  25. var gLockStrs = [];
  26. var gTypeStrs = [];
  27.  
  28. const PREF_IS_DEFAULT_VALUE = 0;
  29. const PREF_IS_USER_SET = 1;
  30. const PREF_IS_LOCKED = 2;
  31.  
  32. var gPrefHash = {};
  33. var gPrefArray = [];
  34. var gPrefView = gPrefArray; // share the JS array
  35. var gFastIndex = 0;
  36. var gSortedColumn = "prefCol";
  37. var gSortFunction = null;
  38. var gSortDirection = 1; // 1 is ascending; -1 is descending
  39. var gConfigBundle = null;
  40.  
  41. var view = {
  42.   get rowCount() { return gPrefView.length; },
  43.   getCellText : function(index, col) {
  44.     if (!(index in gPrefView))
  45.       return "";
  46.     
  47.     var value = gPrefView[index][col.id];
  48.  
  49.     switch (col.id) {
  50.       case "lockCol":           
  51.         return gLockStrs[value];
  52.       case "typeCol":
  53.         return gTypeStrs[value];
  54.       default:
  55.         return value;
  56.     }
  57.   },
  58.   getRowProperties : function(index, prop) {},
  59.   getCellProperties : function(index, col, prop) {
  60.     if (index in gPrefView)
  61.       prop.AppendElement(gLockAtoms[gPrefView[index].lockCol]);
  62.   },
  63.   getColumnProperties : function(col, prop) {},
  64.   treebox : null,
  65.   selection : null,
  66.   isContainer : function(index) { return false; },
  67.   isContainerOpen : function(index) { return false; },
  68.   isContainerEmpty : function(index) { return false; },
  69.   isSorted : function() { return true; },
  70.   canDrop : function(index, orientation) { return false; },
  71.   drop : function(row, orientation) {},
  72.   setTree : function(out) { this.treebox = out; },
  73.   getParentIndex: function(rowIndex) { return -1; },
  74.   hasNextSibling: function(rowIndex, afterIndex) { return false; },
  75.   getLevel: function(index) { return 1; },
  76.   getImageSrc: function(row, col) { return ""; },
  77.   toggleOpenState : function(index) {},
  78.   cycleHeader: function(col) {
  79.     var index = this.selection.currentIndex;
  80.     if (col.id == gSortedColumn)
  81.       gSortDirection = -gSortDirection;
  82.     if (col.id == gSortedColumn && gFastIndex == gPrefArray.length) {
  83.       gPrefArray.reverse();
  84.       if (gPrefView != gPrefArray)
  85.         gPrefView.reverse();
  86.       if (index >= 0)
  87.         index = gPrefView.length - index - 1;
  88.     }
  89.     else {
  90.       var pref = null;
  91.       if (index >= 0) {
  92.         if (gPrefArray != gPrefView)
  93.           index = gPrefView.length - index - 1;
  94.         else
  95.           pref = gPrefArray[index];
  96.       }
  97.       var old = document.getElementById(gSortedColumn);
  98.       old.setAttribute("sortDirection", "");
  99.       gPrefArray.sort(gSortFunction = gSortFunctions[col.id]);
  100.       if (gPrefView != gPrefArray) {
  101.         if (col.id == gSortedColumn)
  102.           gPrefView.reverse();
  103.         else
  104.           gPrefView.sort(gSortFunction);
  105.       }
  106.       gSortedColumn = col.id;
  107.       if (pref)
  108.         index = getIndexOfPref(pref);
  109.     }
  110.     col.element.setAttribute("sortDirection", gSortDirection > 0 ? "ascending" : "descending");
  111.     this.treebox.invalidate();
  112.     if (index >= 0) {
  113.       this.selection.select(index);
  114.       this.treebox.ensureRowIsVisible(index);
  115.     }
  116.     gFastIndex = gPrefArray.length;
  117.   },
  118.   selectionChanged : function() {},
  119.   cycleCell: function(row, col) {},
  120.   isEditable: function(row, col) {return false; },
  121.   setCellValue: function(row, col, value) {},
  122.   setCellText: function(row, col, value) {},
  123.   performAction: function(action) {},
  124.   performActionOnRow: function(action, row) {},
  125.   performActionOnCell: function(action, row, col) {},
  126.   isSeparator: function(index) {return false; }
  127. };
  128.  
  129. // find the index in gPrefView of a pref object
  130. // or -1 if it does not exist in the filtered view
  131. function getViewIndexOfPref(pref)
  132. {
  133.   var low = -1, high = gPrefView.length;
  134.   var index = (low + high) >> 1;
  135.   while (index > low) {
  136.     var mid = gPrefView[index];
  137.     if (mid == pref)
  138.       return index;
  139.     if (gSortFunction(mid, pref) < 0)
  140.       low = index;
  141.     else
  142.       high = index;
  143.     index = (low + high) >> 1;
  144.   }
  145.   return -1;
  146. }
  147.  
  148. // find the index in gPrefArray of a pref object
  149. // either one that was looked up in gPrefHash
  150. // or in case it was moved after sorting
  151. function getIndexOfPref(pref)
  152. {
  153.   var low = -1, high = gFastIndex;
  154.   var index = (low + high) >> 1;
  155.   while (index > low) {
  156.     var mid = gPrefArray[index];
  157.     if (mid == pref)
  158.       return index;
  159.     if (gSortFunction(mid, pref) < 0)
  160.       low = index;
  161.     else
  162.       high = index;
  163.     index = (low + high) >> 1;
  164.   }
  165.  
  166.   for (index = gFastIndex; index < gPrefArray.length; ++index)
  167.     if (gPrefArray[index] == pref)
  168.       break;
  169.   return index;
  170. }
  171.  
  172. function getNearestIndexOfPref(pref)
  173. {
  174.   var low = -1, high = gFastIndex;
  175.   var index = (low + high) >> 1;
  176.   while (index > low) {
  177.     if (gSortFunction(gPrefArray[index], pref) < 0)
  178.       low = index;
  179.     else
  180.       high = index;
  181.     index = (low + high) >> 1;
  182.   }
  183.   return high;
  184. }
  185.  
  186. var gPrefListener =
  187. {
  188.   observe: function(subject, topic, prefName)
  189.   {
  190.     if (topic != "nsPref:changed")
  191.       return;
  192.  
  193.     if (/^capability\./.test(prefName)) // avoid displaying "private" preferences
  194.       return;
  195.  
  196.     var index = gPrefArray.length;
  197.     if (prefName in gPrefHash) {
  198.       index = getViewIndexOfPref(gPrefHash[prefName]);
  199.       fetchPref(prefName, getIndexOfPref(gPrefHash[prefName]));
  200.       if (index >= 0) {
  201.         // Might need to update the filtered view
  202.         gPrefView[index] = gPrefHash[prefName];
  203.         view.treebox.invalidateRow(index);
  204.       }
  205.       if (gSortedColumn == "lockCol" || gSortedColumn == "valueCol")
  206.         gFastIndex = 1; // TODO: reinsert and invalidate range
  207.     } else {
  208.       fetchPref(prefName, index);
  209.       if (index == gFastIndex) {
  210.         // Keep the array sorted by reinserting the pref object
  211.         var pref = gPrefArray.pop();
  212.         index = getNearestIndexOfPref(pref);
  213.         gPrefArray.splice(index, 0, pref);
  214.         gFastIndex = gPrefArray.length;
  215.       }
  216.       if (gPrefView == gPrefArray)
  217.         view.treebox.rowCountChanged(index, 1);
  218.     }
  219.   }
  220. };
  221.  
  222. function prefObject(prefName, prefIndex)
  223. {
  224.   this.prefCol = prefName;
  225. }
  226.  
  227. prefObject.prototype =
  228. {
  229.   lockCol: PREF_IS_DEFAULT_VALUE,
  230.   typeCol: nsIPrefBranch.PREF_STRING,
  231.   valueCol: ""
  232. };
  233.  
  234. function fetchPref(prefName, prefIndex)
  235. {
  236.   var pref = new prefObject(prefName);
  237.  
  238.   gPrefHash[prefName] = pref;
  239.   gPrefArray[prefIndex] = pref;
  240.  
  241.   if (gPrefBranch.prefIsLocked(prefName))
  242.     pref.lockCol = PREF_IS_LOCKED;
  243.   else if (gPrefBranch.prefHasUserValue(prefName))
  244.     pref.lockCol = PREF_IS_USER_SET;
  245.  
  246.   try {
  247.     switch (gPrefBranch.getPrefType(prefName)) {
  248.       case gPrefBranch.PREF_BOOL:
  249.         pref.typeCol = gPrefBranch.PREF_BOOL;
  250.         // convert to a string
  251.         pref.valueCol = gPrefBranch.getBoolPref(prefName).toString();
  252.         break;
  253.       case gPrefBranch.PREF_INT:
  254.         pref.typeCol = gPrefBranch.PREF_INT;
  255.         // convert to a string
  256.         pref.valueCol = gPrefBranch.getIntPref(prefName).toString();
  257.         break;
  258.       default:
  259.       case gPrefBranch.PREF_STRING:
  260.         pref.valueCol = gPrefBranch.getComplexValue(prefName, nsISupportsString).data;
  261.         // Try in case it's a localized string (will throw an exception if not)
  262.         if (pref.lockCol == PREF_IS_DEFAULT_VALUE &&
  263.             /^chrome:\/\/.+\/locale\/.+\.properties/.test(pref.valueCol))
  264.           pref.valueCol = gPrefBranch.getComplexValue(prefName, nsIPrefLocalizedString).data;
  265.         break;
  266.     }
  267.   } catch (e) {
  268.     // Also catch obscure cases in which you can't tell in advance
  269.     // that the pref exists but has no user or default value...
  270.   }
  271. }
  272.  
  273. function onConfigLoad()
  274. {
  275.   // Load strings
  276.   gConfigBundle = document.getElementById("configBundle");
  277.   document.title = gConfigBundle.getString("title");
  278.  
  279.   gLockStrs[PREF_IS_DEFAULT_VALUE] = gConfigBundle.getString("default");
  280.   gLockStrs[PREF_IS_USER_SET] = gConfigBundle.getString("user");
  281.   gLockStrs[PREF_IS_LOCKED] = gConfigBundle.getString("locked");
  282.  
  283.   gTypeStrs[nsIPrefBranch.PREF_STRING] = gConfigBundle.getString("string");
  284.   gTypeStrs[nsIPrefBranch.PREF_INT] = gConfigBundle.getString("int");
  285.   gTypeStrs[nsIPrefBranch.PREF_BOOL] = gConfigBundle.getString("bool");
  286.  
  287.   var prefCount = { value: 0 };
  288.   var prefArray = gPrefBranch.getChildList("", prefCount);
  289.  
  290.   for (var i = 0; i < prefCount.value; ++i) 
  291.   {
  292.     var prefName = prefArray[i];
  293.     if (/^capability\./.test(prefName)) // avoid displaying "private" preferences
  294.       continue;
  295.  
  296.     fetchPref(prefName, gPrefArray.length);
  297.   }
  298.  
  299.   var descending = document.getElementsByAttribute("sortDirection", "descending");
  300.   if (descending.item(0)) {
  301.     gSortedColumn = descending[0].id;
  302.     gSortDirection = -1;
  303.   }
  304.   else {
  305.     var ascending = document.getElementsByAttribute("sortDirection", "ascending");
  306.     if (ascending.item(0))
  307.       gSortedColumn = ascending[0].id;
  308.     else
  309.       document.getElementById(gSortedColumn).setAttribute("sortDirection", "ascending");
  310.   }
  311.   gSortFunction = gSortFunctions[gSortedColumn];
  312.   gPrefArray.sort(gSortFunction);
  313.   gFastIndex = gPrefArray.length;
  314.   
  315.   gPrefBranch.addObserver("", gPrefListener, false);
  316.  
  317.   document.getElementById("configTree").view = view;
  318.   
  319.   document.getElementById("textbox").focus();
  320. }
  321.  
  322. function onConfigUnload()
  323. {
  324.   gPrefBranch.removeObserver("", gPrefListener);
  325.   document.getElementById("configTree").view = null;
  326. }
  327.  
  328. function FilterPrefs()
  329. {
  330.   var substring = document.getElementById("textbox").value.toLowerCase();
  331.   var prefCol = view.selection.currentIndex < 0 ? null : gPrefView[view.selection.currentIndex].prefCol;
  332.   var array = gPrefView;
  333.   gPrefView = gPrefArray;
  334.   if (substring) {
  335.     gPrefView = [];
  336.     for (var i = 0; i < gPrefArray.length; ++i)
  337.       if (gPrefArray[i].prefCol.toLowerCase().indexOf(substring) >= 0)
  338.         gPrefView.push(gPrefArray[i]);
  339.     if (gFastIndex < gPrefArray.length)
  340.       gPrefView.sort(gSortFunction);
  341.   }
  342.   view.treebox.invalidate();
  343.   view.treebox.rowCountChanged(array.length, gPrefView.length - array.length);
  344.   gotoPref(prefCol);
  345.   document.getElementById("button").disabled = !substring;
  346. }
  347.  
  348. function ClearFilter(button)
  349. {
  350.   var textbox = document.getElementById("textbox");
  351.   textbox.value = "";
  352.   textbox.focus();
  353.   FilterPrefs();
  354. }
  355.  
  356. function prefColSortFunction(x, y)
  357. {
  358.   if (x.prefCol > y.prefCol)
  359.     return gSortDirection;
  360.   if (x.prefCol < y.prefCol) 
  361.     return -gSortDirection;
  362.   return 0;
  363. }
  364.  
  365. function lockColSortFunction(x, y)
  366. {
  367.   if (x.lockCol != y.lockCol)
  368.     return gSortDirection * (y.lockCol - x.lockCol);
  369.   return prefColSortFunction(x, y);
  370. }
  371.  
  372. function typeColSortFunction(x, y)
  373. {
  374.   if (x.typeCol != y.typeCol) 
  375.     return gSortDirection * (y.typeCol - x.typeCol);
  376.   return prefColSortFunction(x, y);
  377. }
  378.  
  379. function valueColSortFunction(x, y)
  380. {
  381.   if (x.valueCol > y.valueCol)
  382.     return gSortDirection;
  383.   if (x.valueCol < y.valueCol) 
  384.     return -gSortDirection;
  385.   return prefColSortFunction(x, y);
  386. }
  387.  
  388. const gSortFunctions =
  389. {
  390.   prefCol: prefColSortFunction, 
  391.   lockCol: lockColSortFunction, 
  392.   typeCol: typeColSortFunction, 
  393.   valueCol: valueColSortFunction
  394. };
  395.  
  396. function updateContextMenu()
  397. {
  398.   var lockCol = PREF_IS_LOCKED;
  399.   var typeCol = nsIPrefBranch.PREF_STRING;
  400.   var valueCol = "";
  401.   var copyDisabled = true;
  402.  
  403.   if (view.selection.currentIndex >= 0) {
  404.     var prefRow = gPrefView[view.selection.currentIndex];
  405.     lockCol = prefRow.lockCol;
  406.     typeCol = prefRow.typeCol;
  407.     valueCol = prefRow.valueCol;
  408.     copyDisabled = false;
  409.   }
  410.  
  411.   var copyName = document.getElementById("copyName");
  412.   copyName.setAttribute("disabled", copyDisabled);
  413.  
  414.   var copyValue = document.getElementById("copyValue");
  415.   copyValue.setAttribute("disabled", copyDisabled);
  416.  
  417.   var resetSelected = document.getElementById("resetSelected");
  418.   resetSelected.setAttribute("disabled", lockCol != PREF_IS_USER_SET);
  419.  
  420.   var canToggle = typeCol == nsIPrefBranch.PREF_BOOL && valueCol != "";
  421.  
  422.   var modifySelected = document.getElementById("modifySelected");
  423.   modifySelected.setAttribute("disabled", lockCol == PREF_IS_LOCKED);
  424.   modifySelected.hidden = canToggle;
  425.  
  426.   var toggleSelected = document.getElementById("toggleSelected");
  427.   toggleSelected.setAttribute("disabled", lockCol == PREF_IS_LOCKED);
  428.   toggleSelected.hidden = !canToggle;
  429.   
  430.   var entry = gPrefView[view.selection.currentIndex];
  431.   var isLocked = gPrefBranch.prefIsLocked(entry.prefCol);
  432.  
  433.   // These might not exist (bug 289136)
  434.   try {
  435.     document.getElementById("lockSelected").hidden = isLocked;
  436.     document.getElementById("unlockSelected").hidden = !isLocked;
  437.   } catch (ex) {}
  438. }
  439.  
  440. function copyName()
  441. {
  442.   gClipboardHelper.copyString(gPrefView[view.selection.currentIndex].prefCol);
  443. }
  444.  
  445. function copyValue()
  446. {
  447.   gClipboardHelper.copyString(gPrefView[view.selection.currentIndex].valueCol);
  448. }
  449.  
  450. function ModifySelected()
  451. {
  452.   if (view.selection.currentIndex >= 0)
  453.     ModifyPref(gPrefView[view.selection.currentIndex]);
  454. }
  455.  
  456. function ResetSelected()
  457. {
  458.   var entry = gPrefView[view.selection.currentIndex];
  459.   gPrefBranch.clearUserPref(entry.prefCol);
  460. }
  461.  
  462. function NewPref(type)
  463. {
  464.   var result = { value: "" };
  465.   var dummy = { value: 0 };
  466.   if (gPromptService.prompt(window,
  467.                             gConfigBundle.getFormattedString("new_title", [gTypeStrs[type]]),
  468.                             gConfigBundle.getString("new_prompt"),
  469.                             result,
  470.                             null,
  471.                             dummy) && result.value) {
  472.     var pref;
  473.     if (result.value in gPrefHash)
  474.       pref = gPrefHash[result.value];
  475.     else
  476.       pref = { prefCol: result.value, lockCol: PREF_IS_DEFAULT_VALUE, typeCol: type, valueCol: "" };
  477.     if (ModifyPref(pref))
  478.       setTimeout(gotoPref, 0, result.value);
  479.   }
  480. }
  481.  
  482. function gotoPref(pref)
  483. {
  484.   // make sure the pref exists and is displayed in the current view
  485.   var index = pref in gPrefHash ? getViewIndexOfPref(gPrefHash[pref]) : -1;
  486.   if (index >= 0) {
  487.     view.selection.select(index);
  488.     view.treebox.ensureRowIsVisible(index);
  489.   } else {
  490.     view.selection.clearSelection();
  491.     view.selection.currentIndex = -1;
  492.   }
  493. }
  494.  
  495. function ModifyPref(entry)
  496. {
  497.   if (entry.lockCol == PREF_IS_LOCKED)
  498.     return false;
  499.   var title = gConfigBundle.getFormattedString("modify_title", [gTypeStrs[entry.typeCol]]);
  500.   if (entry.typeCol == nsIPrefBranch.PREF_BOOL) {
  501.     var check = { value: entry.valueCol == "false" };
  502.     if (!entry.valueCol && !gPromptService.select(window, title, entry.prefCol, 2, [false, true], check))
  503.       return false;
  504.     gPrefBranch.setBoolPref(entry.prefCol, check.value);
  505.   } else {
  506.     var result = { value: entry.valueCol };
  507.     var dummy = { value: 0 };
  508.     if (!gPromptService.prompt(window, title, entry.prefCol, result, null, dummy))
  509.       return false;
  510.     if (entry.typeCol == nsIPrefBranch.PREF_INT) {
  511.       gPrefBranch.setIntPref(entry.prefCol, parseInt(result.value, 10));
  512.     } else {
  513.       var supportsString = Components.classes[nsSupportsString_CONTRACTID].createInstance(nsISupportsString);
  514.       supportsString.data = result.value;
  515.       gPrefBranch.setComplexValue(entry.prefCol, nsISupportsString, supportsString);
  516.     }
  517.   }
  518.   
  519.   gPrefService.savePrefFile(null);
  520.   
  521.   // Fire event for accessibility
  522.   var event = document.createEvent('Events');
  523.   event.initEvent('NameChange', false, true);
  524.   document.getElementById("configTree").dispatchEvent(event);
  525.  
  526.   return true;
  527. }
  528.  
  529. function LockSelected()
  530. {
  531.   var entry = gPrefView[view.selection.currentIndex];
  532.   gPrefBranch.lockPref(entry.prefCol);
  533. }
  534.  
  535. function UnlockSelected()
  536. {
  537.   var entry = gPrefView[view.selection.currentIndex];
  538.   gPrefBranch.unlockPref(entry.prefCol);
  539. }
  540.  
  541.